package com.qijian.maindata.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.SecurityUtils;
import com.qijian.common.utils.bean.BeanUtils;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.maindata.domain.AdjustFields;
import com.qijian.maindata.domain.query.AdjustFieldsQuery;
import com.qijian.maindata.domain.vo.AdjustFieldsVo;
import com.qijian.maindata.service.IAdjustFieldsService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 调整字段Controller
 *
 * @author qijian
 * @date 2022-10-14
 */
@Api(tags = "调整字段")
@RestController
@RequiredArgsConstructor
@RequestMapping("/business/fields")
public class AdjustFieldsController extends BaseController {

    private final IAdjustFieldsService adjustFieldsService;

    /**
     * 查询调整字段列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AdjustFieldsQuery query, Page<AdjustFields> page) {
        initOrderItem(AdjustFields.class, page.orders());
        return getDataTable(adjustFieldsService.page(page, WhereEntityTool.invoke(query)));
    }

    @GetMapping("/selectAdjustedFields")
    public AjaxResult selectAdjustedFields(AdjustFieldsQuery query) {
        String adjustedFieldsList = adjustFieldsService.selectAdjustedFields(WhereEntityTool.invoke(query));
        return AjaxResult.data(adjustedFieldsList);
    }

    /**
     * 新增调整字段
     */
    @Log(title = "调整字段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdjustFieldsVo adjustFieldsVo) {
        AdjustFields adjustFields = new AdjustFields();
        BeanUtils.copyProperties(adjustFieldsVo, adjustFields);

        long id = Long.parseLong(String.format("%s%s", adjustFieldsVo.getUserId(), adjustFieldsVo.getMenuId()));
        adjustFields.setId(id);
        adjustFields.setAdjustedFields(adjustFieldsVo.getColumns());

        return toAjax(adjustFieldsService.saveOrUpdate(adjustFields));
    }

    /**
     * 删除调整字段
     */
    @Log(title = "调整字段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{menuId}")
    public AjaxResult remove(@PathVariable Long menuId) {
        adjustFieldsService.deleteById(String.format("%s%s", SecurityUtils.getUserId(), menuId));
        return toAjax(true);
    }

    /**
     * 修改调整字段
     */
    @Log(title = "调整字段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdjustFields adjustFields) {
        return toAjax(adjustFieldsService.updateById(adjustFields));
    }

}
